package com.baidu.customview.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.baidu.customview.R;
import com.baidu.customview.utils.MetricsUtils;

/**
 * Created by imagine on 2015/11/14.
 * 总结：千万要注意android中控件位位置都是相对父容器的，如此自定义View里面的画布，画布左上角顶点就是新坐标系的(0,0)(对子而言)
 */
public class CircleRectImg extends View {
    private Canvas mCanvas;
    private Paint p;
    private int poiLeft, poiTop, poiRight, poiBottom, poiRadius;//矩形圆角边圆心的坐标，
    private int margin = 10;
    private int picX, picY;//图片放置左上角点的坐标
    private int picW, picH;//图片长和宽
    private Bitmap bg;
    private Bitmap fg;

    public CircleRectImg(Context context) {
        super(context);
    }

    public CircleRectImg(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.FILL);
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        p.setColor(getResources().getColor(R.color.transparent));//初始化画笔，注意颜色为透明

        bg = BitmapFactory.decodeResource(getResources(), R.mipmap.peien);
        picX = margin;
        picY = margin;
        picW = MetricsUtils.winMetrics(context).widthPixels - 2 * margin;
        picH = bg.getHeight();
//        picW = 300;
//        picH = 200;
        bg = bg.createScaledBitmap(bg, picW, picH, false);

        fg = Bitmap.createBitmap(picW, picH, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(fg);
        mCanvas.drawColor(getResources().getColor(R.color.white));

        poiRadius = 20;
        poiLeft = poiRadius;
        poiTop = poiRadius;
        poiRight = picW - poiRadius;
        poiBottom = picH - poiRadius;
    }

    public CircleRectImg(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CircleRectImg(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.setDensity(DRAWING_CACHE_QUALITY_AUTO);
        canvas.drawBitmap(bg, picX, picY, null);//设置背景
        canvas.drawBitmap(fg, picX, picY, null);//设置背景
//        //画出要显示的部分
//
        mCanvas.drawCircle(poiLeft, poiTop, poiRadius, p);
        mCanvas.drawCircle(poiLeft, poiBottom, poiRadius, p);
        mCanvas.drawCircle(poiRight, poiTop, poiRadius, p);
        mCanvas.drawCircle(poiRight, poiBottom, poiRadius, p);
        mCanvas.drawRect(0, poiRadius, picW, picH - poiRadius, p);
        mCanvas.drawRect(poiRadius, 0, picW - poiRadius, picH, p);
    }
}
